#include "dstat.h"
#include "nfa.h"
#include <fstream>
#include <set>
#include <map>
#include <vector>

std::map<Node*, int> labels;
int node_label = 0;

int Dstat::label_count = 1;

Dstat::Dstat() : refer_count(nullptr), label(0)
{
	stats = new Bitmap(256);
	refer_count = new int;
	*refer_count = 0;
}
Dstat::Dstat(const Dstat& stat) : label(stat.label), refer_count(stat.refer_count), stats(stat.stats), nodes(stat.nodes)
{
	(*refer_count)++;
}
Dstat::~Dstat()
{
	if (!refer_count)
		delete stats;
	else
		(*refer_count)--;
}

void Dstat::genLabel()
{
	this->label = label_count++;
}

bool Dstat::havaFinalStat()
{
	return !this->finalStat.empty();
}

bool operator<(const Dstat& l,const Dstat& other)
{
	return  l.stats->less(*other.stats);
}

void Dstat::addStat(Node* node)
{
	nodes.insert(node);
	if (node->type == Node::END_TYPE)
	{
		finalStat.push_back(node);
	}
	if (labels.find(node) == labels.end())
	{
		labels[node] = node_label;
		stats->setBit(node_label);
		++node_label;
	} 
	else
	{
		stats->setBit(labels[node]);
	}
}
